<template>
    <div>
        <!-- 查询条件开始 -->
        <el-form ref="search" :model="search" :inline="true" label-width="68px">
            <el-form-item label="用户名称" prop="userName">
                <el-input
                        v-model="search.userName"
                        placeholder="请输入用户名称"
                        clearable
                        size="small"
                        style="width:200px"/>
            </el-form-item>
            <el-form-item label="用户账号" prop="loginAccount">
                <el-input
                        v-model="search.loginAccount"
                        placeholder="请输入用户账号"
                        clearable
                        size="small"
                        style="width:200px"/>
            </el-form-item>
            <el-form-item label="IP地址" prop="ipAddr">
                <el-input
                        v-model="search.ipAddr"
                        placeholder="请输入IP地址"
                        clearable
                        size="small"
                        style="width:200px"/>
            </el-form-item>
            <el-form-item label="登陆状态" prop="loginStatus">
                <el-select
                        v-model="search.loginStatus"
                        placeholder="请选择登陆状态"
                        clearable
                        size="small"
                        style="width:200px">
                    <el-option
                            v-for="dict in statusOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"/>
                </el-select>
            </el-form-item>
            <el-form-item label="登陆类型" prop="loginType">
                <el-select
                        v-model="search.loginType"
                        placeholder="请选择登陆类型"
                        clearable
                        size="small"
                        style="width:200px">
                    <el-option
                            v-for="dict in loginTypeOptions"
                            :key="dict.dictValue"
                            :label="dict.dictLabel"
                            :value="dict.dictValue"/>
                </el-select>
            </el-form-item>
            <el-form-item label="创建时间">
                <el-date-picker
                        v-model="search.dateRange"
                        size="small"
                        style="width:240px"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholde="开始日期"
                        end-placeholde="结束日期"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button type="normal" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 查询条件结束 -->
        <!-- 表头按钮开始 -->
        <el-row :gutter="10" style="margin-bottom: 8px;">
            <el-col :span="1.5">
                <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">批量删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-thumb" size="mini" @click="handleClearInfo">清空</el-button>
            </el-col>
        </el-row>
        <!-- 表头按钮结束 -->

        <!-- 数据表格开始 -->
        <el-table border :data="loginInfoTableList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="日志ID" align="center" prop="infoId" />
            <el-table-column label="用户姓名" align="center" prop="userName" />
            <el-table-column label="登陆帐号" align="center" prop="loginAccount" />
            <el-table-column label="IP" width="180" align="center" prop="ipAddr" />
            <el-table-column label="登陆地址" align="center" prop="loginLocation" />
            <el-table-column label="浏览器" align="center" prop="browser" />
            <el-table-column label="操作系统" align="center" prop="os" />
            <el-table-column label="登陆状态" prop="loginStatus" align="center" :formatter="statusFormatter" />
            <el-table-column label="用户类型" prop="loginType" align="center" :formatter="loginTypeFormatter" />
            <el-table-column label="登陆时间" align="center" prop="loginTime" width="200" />
            <el-table-column label="操作" align="center" width="100">
                <template slot-scope="scope">
                    <el-button v-if="scope.row.userId!=1" type="text" icon="el-icon-delete" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 数据表格结束 -->

        <!--  分页-->
        <el-pagination
                v-show="page.total>0"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page.current"
                :page-sizes="[10, 20, 30,40]"
                :page-size="page.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total">
        </el-pagination>
        <!--  分页结束-->
    </div>
</template>

<script>
    export default {
        name: "LoginLog",
        data(){
            return{
                //条件查询
                search:{},
                //禁用按钮 批量删除
                multiple:true,
                //存储多选框数据
                LogsIds:[],
                //登录状态数据
                statusOptions:[],
                //登录类型数据
                loginTypeOptions:[],
                //表格数据
                loginInfoTableList:[],
                //分页
                page:{
                    current:1,
                    size:10,
                    total:10
                }
            }
        },
        methods:{
            //模糊查询
            handleQuery(){
                this.page.current=1;
                this.initloginLogData();
            },
            //重置查询
            resetQuery(){
                this.search={};
            },
            //分页 每页显示的数量
            handleSizeChange(val){
                this.page.size=val;
                this.initloginLogData();
            },
            //分页 显示的当前页数
            handleCurrentChange(val){
                this.page.current=val;
                this.initloginLogData();
            },
            //多选框触发事件
            handleSelectionChange(LogSelection){
               this.LogsIds=LogSelection.map(item=>item.infoId);
               this.multiple = !LogSelection.length;
            },
            //批量删除
            handleDelete(row){
                const infoIds=row.infoId || this.LogsIds;
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.delete("system/api/loginLog/delLoginLog/"+infoIds).then(r=>{
                        if (r.data.t){
                            this.$message.success(r.data.msg);
                        }else {
                            this.$message.error("操作失败");
                        }
                        this.page.current=1;
                        this.initloginLogData();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //清空表单
            handleClearInfo(){
                this.$confirm('此操作将清除所有数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.delete("system/api/loginLog/clearTable").then(r=>{
                        this.$message.success(r.data.msg);
                        this.initloginLogData();
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //加载表格数据
            initloginLogData(){
                const params=Object.assign(this.search,this.page)
                this.$axios.post("system/api/loginLog/getAllLog",params).then(r=>{
                    this.loginInfoTableList=r.data.t.records;
                    this.page.total=r.data.t.total;
                })
            },
            //加载状态字典
            initStatusLog(){
                this.$axios.get("system/api/dict/data/findByType/sys_common_status").then(r=>{
                    this.statusOptions=r.data.t;
                })
            },
            //状态格式解析
            statusFormatter(row){
                var val = row.loginStatus;
                return this.formatDict(this.statusOptions,val);
            },
            //加载登录类型字典
            initLogTyp(){
                this.$axios.get("system/api/dict/data/findByType/sys_user_type").then(r=>{
                    this.loginTypeOptions=r.data.t;
                })
            },
            //格式解析用户类型字典
            loginTypeFormatter(row){
                var val = row.loginType;
                return this.formatDict(this.loginTypeOptions,val);
            },
        },
        created() {
            //加载表格数据
            this.initloginLogData();
            //加载状态字典
            this.initStatusLog();
            //加载用户类型字典
            this.initLogTyp();
        }
    }
</script>

<style scoped>

</style>
